// 引入express
const express = require('express')
// 创建服务器
const app = express()
// 引入mysql
const mysql = require('mysql')
// 连接数据库
const conn = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: "三级联动"
})
// 托管views文件
app.use('/', express.static('./views'))
// 获取省份数据
app.get('/province', (req, res) => {
    // res.send('123')
    const sql1 = "select * from province"
    conn.query(sql1, (err, result) => {
        if (err) return res.send({msg:'错误', status: 501})
        res.send({status:200, data: result})
    })
})
// 获取市数据
app.get('/city', (req, res) => {
    // res.send('123')
    const p_id = req.query.p_id
    
    const sql2 = "select * from city where p_id=?"
    conn.query(sql2, p_id,  (err, result) => {
        if (err) return res.send({msg:'错误', status: 501})
        res.send({status:200, data: result})
    })
})
// 获取区数据
app.get('/area', (req, res) => {
    // res.send('123')
    const c_id = req.query.c_id
    
    const sql3 = "select * from area where c_id=?"
    conn.query(sql3, c_id,  (err, result) => {
        if (err) return res.send({msg:'错误', status: 501})
        res.send({status:200, data: result})
    })
})

app.listen('1234', () => {
    console.log('http://127.0.0.1:1234');
    
})